import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_model/home/home_tabs_entity.dart';
import 'package:tiktokadult/hj_model/home/module_detail_model.dart';
import 'package:tiktokadult/hj_page/cartoon_business/cartoon_section_types.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/banner/ads_grid_view_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/refresh/pull_refresh.dart';

import '../home_business/home_cell_style/home_jg_area_widget.dart';
import 'cartoon_sub_module_page.dart';
import 'cartoon_sub_section_logic.dart';

//专题样式
class CartoonSubSectionPage extends StatefulWidget {
  final ModuleData? tagData;
  final CommunityType? type;
  const CartoonSubSectionPage({
    super.key,
    this.tagData,
    this.type,
  });

  @override
  State<CartoonSubSectionPage> createState() => _CartoonSubSectionPageState();
}

class _CartoonSubSectionPageState extends State<CartoonSubSectionPage> {
  late CartoonSubSectionLogic logic;

  @override
  void initState() {
    super.initState();
    logic = Get.put(
      CartoonSubSectionLogic(tagData: widget.tagData),
      tag: widget.tagData?.id ?? '',
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CartoonSubSectionLogic>(
      tag: widget.tagData?.id ?? '',
      builder: (_) => pullYsRefresh(
        onInit: (ctr) => _.refrshCtr = ctr,
        onRefresh: (ctr) => _.loadSectionTypeData(),
        onLoading: (ctr) => _.loadMoreData(),
        child: CustomScrollView(
          controller: logic.scl,
          slivers: _buildScrollItems(),
        ),
      ),
    );
  }

  //专题样式
  _buildScrollItems() {
    List<Widget> items = [SliverToBoxAdapter(child: 12.sizeBoxH)];
    items.add(
      SliverToBoxAdapter(
        child: AdsGridViewWidget(
          widget.type == CommunityType.TextNovel ? 7 : 4,
          isBigStyle: true,
          padding: EdgeInsets.fromLTRB(16, 0, 16, 12),
        ),
      ),
    );
    if (widget.type == CommunityType.Cartoon) {
      items.add(SliverToBoxAdapter(child: HomeJgAreaWidget(widget.tagData)));
    }
    if (logic.sectionList == null) {
      items.add(SliverToBoxAdapter(
        child: LoadingCenterWidget(height: 400),
      ));
    } else if (logic.sectionList?.isNotEmpty == true) {
      for (int i = 0; i < logic.sectionList!.length; i++) {
        AllSection element = logic.sectionList![i];
        int? showType = element.showType ?? -1;

        print('_buildScrollItems ======= ${element.showType}, ${element.sectionName}');
        if (element.isAdsArr() == true) {
          items.add(SliverToBoxAdapter(
            child: AdsGridViewWidget(
              -1,
              adsArr: element.adsInfoArr,
              padding: EdgeInsets.only(left: 16, top: 12, right: 16),
            ),
          ));
        } else if (showType == 3 || (showType <= 11 && showType >= 7)) {
          items.add(CarttonSectionTypes[showType]!.renderType(element, logic));
        } else {
          items.add(CarttonSectionTypes[3]!.renderType(element, logic));
        }
      }
      if (logic.preferenceInfo != null) {
        items.add(
          SliverToBoxAdapter(
            child: GetBuilder<CartoonSubSectionLogic>(
              tag: widget.tagData?.id ?? '',
              id: 'recommend',
              builder: (controller) => PreferencerGrid(
                likeLoading: logic.likeLoading,
                preferencesModel: logic.preferenceInfo,
                guessMediaInfoList: logic.preferenceList,
                scl: logic.scl,
                selectIndex: logic.selectIndex.value,
                onChangeRecomendSort: (index) => logic.onChangeRecomendSort(index),
                onInitOffset: (dy) => logic.onInitRecomendDy(dy),
                loadData: () => logic.loadData(),
              ),
            ),
          ),
        );
      }
    }

    return items;
  }
}
